package org.smile.http.client;

import org.smile.commons.SmileRunException;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class DefaultHttpResponse extends AbstractHttpResponse<ByteBuffer> {
	//响应body读取的内容
	protected ByteBuffer byteContent;

	@Override
	public void receiveContent(InputStream is, int maxBodySize) throws IOException {
		byteContent= HttpDataUtils.readToByteBuffer(is, maxBodySize);
	}
	
	@Override
	public String readBody() throws UnsupportedEncodingException {
		if(this.byteContent==null){
			throw new SmileRunException("response body is empty");
		}
		return new String(byteContent.array(),charset);
	}
	
	@Override
	public ByteBuffer getContent() {
		return byteContent.duplicate();
	}
}
